﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLSharp;

public partial class _Default : System.Web.UI.Page 
{
    public string CookieUserName = "";
    public string CookiePassword = "";
    public string AppGuid = System.Configuration.ConfigurationSettings.AppSettings["AppGuid"];
    public TLSharp.BrowseMyGameResponse MyGames = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Request.Browser.Type.ToLower().Contains("firefox") || Request.Browser.Type.ToLower().StartsWith("ie")) && Request["bypass"] == null && Request["bypass"] != "true")
        {
            Response.Redirect("LandingPage.htm");
        }
        CookieUserName = Request.Cookies["TLUsername"] != null ? Request.Cookies["TLUsername"].Value : "";
        CookiePassword = Request.Cookies["TLPassword"] != null ? Request.Cookies["TLPassword"].Value : "";

        if (CookieUserName.Length > 0 && CookiePassword.Length > 0)
        {
            MyGames = TLSharp.TouchLeagueService.BrowseGames(CookieUserName, CookiePassword, AppGuid);
            if (MyGames.ErrorCode >= 0)
            {
                MyGamesLink.Visible = true;
                FindMatchLink.Visible = true;
                LadderLink.Visible = true;
                lblStartHere.Visible = false;
                SignUpLink.Visible = false;
                lblThenHere.Visible = false;

                if (MyGames.ActiveGames.Count > 0)
                {
                    ActiveGameTag.InnerHtml = MyGames.ActiveGames.Count.ToString();
                    ActiveGameTag2.InnerHtml = MyGames.ActiveGames.Count.ToString();
                    foreach (TLSharp.Game ThisGame in MyGames.ActiveGamesObjects)
                    {
                        pnGames.Controls.Add(new LiteralControl("<li><a href='Game.aspx?GameID=" + ThisGame.GameID.ToString() + "'> <table><tr><td>#" + ThisGame.Challengee.CurrentRank.ToString() + " " + ThisGame.Challengee.QuickName + "</td><td>" + ThisGame.Challengee.CurrentScore + "</td></tr><tr><td>#" + ThisGame.Challenger.CurrentRank.ToString() + " " + ThisGame.Challenger.QuickName + "</td><td>" + ThisGame.Challenger.CurrentScore + "</td></tr></table></a></li>"));
                    }
                }
                else
                {
                    ActiveGameTag.Visible = false;
                    ActiveGameTag2.Visible = false;
                }
                if (MyGames.WaitingGames.Count > 0)
                {
                    foreach (TLSharp.Game ThisGame in MyGames.WaitingGamesObjects)
                    {
                        pnWaitingGames.Controls.Add(new LiteralControl("<li><a href='Game.aspx?GameID=" + ThisGame.GameID.ToString() + "'> <table><tr><td>#" + ThisGame.Challengee.CurrentRank.ToString() + " " + ThisGame.Challengee.QuickName + "</td><td>" + ThisGame.Challengee.CurrentScore + "</td></tr><tr><td>#" + ThisGame.Challenger.CurrentRank.ToString() + " " + ThisGame.Challenger.QuickName + "</td><td>" + ThisGame.Challenger.CurrentScore + "</td></tr></table></a></li>"));
                    }
                }
                if (MyGames.Results.Count > 0)
                {
                    foreach (TLSharp.Game ThisGame in MyGames.ResultObjects)
                    {
                        pnResults.Controls.Add(new LiteralControl("<li><a href='Game.aspx?GameID=" + ThisGame.GameID.ToString() + "'> <table><tr><td>#" + ThisGame.Challengee.CurrentRank.ToString() + " " + ThisGame.Challengee.QuickName + "</td><td>" + ThisGame.Challengee.CurrentScore + "</td></tr><tr><td>#" + ThisGame.Challenger.CurrentRank.ToString() + " " + ThisGame.Challenger.QuickName + "</td><td>" + ThisGame.Challenger.CurrentScore + "</td></tr></table></a></li>"));
                    }
                }
                if (MyGames.Requests.Count > 0)
                {
                    ActiveRequestsTag.InnerHtml = MyGames.Requests.Count.ToString();
                    foreach (TLSharp.GameRequest ThisGame in MyGames.RequestObjects)
                    {
                        if (ThisGame.TargetTeamID > 0)
                        {
                            //team challenge                           
                            pnRequests.Controls.Add(new LiteralControl(String.Format("<li><a href='RemoveRequest.aspx?GameRequestID={0}&TargetTeamID={1}&TeamName={2}&CurrentRank={3}'>", ThisGame.GameRequestID.ToString(), ThisGame.TargetTeamID.ToString(), ThisGame.TargetTeamObject.TeamGeo + " " + ThisGame.TargetTeamObject.TeamMascot, ThisGame.TargetTeamObject.CurrentRank.ToString()) + "You Challenged: #" + ThisGame.TargetTeamObject.CurrentRank.ToString() + " " + ThisGame.TargetTeamObject.TeamGeo + " " + ThisGame.TargetTeamObject.TeamMascot + ".  Click here to delete.</a></li>"));
                        }
                        else
                        {
                            pnRequests.Controls.Add(new LiteralControl(String.Format("<li><a href='RemoveRequest.aspx?GameRequestID={0}&MinRank={1}&MaxRank={2}'>", ThisGame.GameRequestID.ToString(), ThisGame.MinRank.ToString(), ThisGame.MaxRank.ToString()) + "You Requested: " + (ThisGame.MinRank > 0 ? "#" + ThisGame.MinRank.ToString() + " to " : " Up to ") + (ThisGame.MaxRank > 0 ? "# " + ThisGame.MaxRank.ToString() : " the end") + " Click here to delete.</a></li>"));
                        }
                    }
                }
                else
                {
                    ActiveRequestsTag.Visible = false; ;
                    
                }
                if (MyGames.Challenges.Count > 0)
                {
                    ActiveChallengesTag.InnerHtml = MyGames.Challenges.Count.ToString();
                    ActiveChallengesTag2.InnerHtml = MyGames.Challenges.Count.ToString();
                    foreach (TLSharp.GameRequest ThisGame in MyGames.ChallengeObjects)
                    {
                        //team challenge
                        this.pnActiveChallenges.Controls.Add(new LiteralControl(String.Format("<li><a href='ChallengeProcess.aspx?&TargetTeamID={0}&bChallenge={1}'>", ThisGame.RequestingTeamID.ToString(), (ThisGame.bChallenge ? "on" : "")) + "Accept Challenge from: #" + ThisGame.ChallengingTeamObject.CurrentRank.ToString() + " " + ThisGame.ChallengingTeamObject.TeamGeo + " " + ThisGame.ChallengingTeamObject.TeamMascot + " in a " + (ThisGame.bChallenge ? "" : "non-") + " challenge game.</a></li>"));
                        this.pnActiveChallenges.Controls.Add(new LiteralControl(String.Format("<li style='background-color:#F29A9A;'><a href='RemoveRequest.aspx?&GameRequestID={0}'>", ThisGame.GameRequestID.ToString()) + "Decline Challenge from: #" + ThisGame.ChallengingTeamObject.CurrentRank.ToString() + " " + ThisGame.ChallengingTeamObject.TeamGeo + " " + ThisGame.ChallengingTeamObject.TeamMascot + " in a " + (ThisGame.bChallenge ? "" : "non-") + " challenge game.</a></li>"));
                    }
                }
                else
                {
                    ActiveChallengesTag.Visible = false;
                    ActiveChallengesTag2.Visible = false;
                }

                double LadderStartRank = MyGames.CurrentRank - 25;
                if (LadderStartRank < 1)
                {
                    LadderStartRank = 1;
                }
                ViewLadderLink.HRef = "Ladder.aspx?ThisTeamID=" + MyGames.ThisTeamID + "&LadderStartRank=" + LadderStartRank.ToString();
            }
            else
            {
                this.TeamProfileLink.Visible = false;
                MyGamesLink.Visible = false;
                FindMatchLink.Visible = false;
                LadderLink.Visible = false;
                lblStartHere.Visible = true;
                SignUpLink.Visible = true;
                lblThenHere.Visible = true;


            }
        }
        else
        {
            this.TeamProfileLink.Visible = false;
            MyGamesLink.Visible = false;
            FindMatchLink.Visible = false;
            LadderLink.Visible = false;

            lblStartHere.Visible = true;
            SignUpLink.Visible = true;
            lblThenHere.Visible = true;
        }
    }
}
